package edu.utn.tacs.grupouno.ui.client;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import edu.utn.tacs.grupouno.ui.client.dto.AnalisisDTO;

public class AnalisisPanel extends VerticalPanel {
	
	private String pacienteId;
	private Panel panelEncabezado;
	private Panel panelLista;
	private Panel panelDetalle;
	
	public AnalisisPanel() {
		initComponents();
	}
	
	public AnalisisPanel(String pacienteId) {
		this.pacienteId = pacienteId;
		initComponents();
	}

	private void initComponents() {
		setSpacing(5);
		panelLista = new VerticalPanel();
		panelEncabezado = new VerticalPanel();
		panelDetalle = new VerticalPanel();
		
		panelEncabezado.add(new Label("Cargando..."));
		HorizontalPanel container = new HorizontalPanel();
		container.setSpacing(4);
		container.add(panelLista);
		container.add(panelDetalle);
		add(panelEncabezado);
		add(container);
		if(pacienteId != null) {
			initAnalisisFromPaciente();
		} else {
			initAllAnalisis();
		}
	}
	private void initAnalisisFromPaciente() {
		AnalisisUIServiceAsync service = GWT.create(AnalisisUIService.class);
		service.getAnalisisFromPaciente(pacienteId, new PacienteAsyncCallback());
	}
	private void initAllAnalisis() {
		add(new Label("Elija un paciente para ver sus analisis"));
	}
	
	private final class PacienteAsyncCallback implements AsyncCallback<List<AnalisisDTO>> {
		private PacienteAsyncCallback() {
		}

		@Override
		public void onSuccess(List<AnalisisDTO> analisis) {
			panelEncabezado.clear();
			if(analisis.isEmpty()) {
				panelEncabezado.add(new Label("No hay analisis para este paciente."));
				return;
			}
			
			Label label = new Label("Seleccione un analisis");
			label.setStyleName("title");
			panelEncabezado.add(label);
			
			Grid grid = new Grid(analisis.size() + 1, 4);
			grid.setStyleName("titledGrid");
			Label numTitle = new Label("#");
			Label medTitle = new Label("Medico");
			numTitle.setStyleName("title");
			medTitle.setStyleName("title");
			grid.setWidget(0, 0, numTitle);
			grid.setWidget(0, 1, medTitle);
			
			int i = 1;
			for(AnalisisDTO analis : analisis) {
				grid.setWidget(i, 0, new Label(String.valueOf(i)));
				grid.setWidget(i, 1, new Label(analis.getNombreMedico()));
				grid.setWidget(i, 2, newAsignButton(analis));
				grid.setWidget(i, 3, newDetailsButton(analis));
				i++;
			}
			panelLista.add(grid);
		}
		
		private Widget newDetailsButton(AnalisisDTO analis) {
			return new Button("Detalles >>", new AnalisisDetalleClickHandler(analis.getId()));
		}
		
		private Widget newAsignButton(AnalisisDTO analis) {
			return new Button("Asignar turno", new AsignTurnoClickHandler(analis));
		}
		
		@Override
		public void onFailure(Throwable arg0) {
			panelEncabezado.clear();
			Label label = new Label("Error: " + arg0.getMessage());
			label.setStyleName("error");
			panelEncabezado.add(label);
		}
	}

	private class AnalisisDetalleClickHandler implements ClickHandler {
		private String id;
		public AnalisisDetalleClickHandler(String id) {
			this.id = id;
		}
		@Override
		public void onClick(ClickEvent arg0) {
			panelDetalle.clear();
			panelDetalle.setStyleName("titledGrid");
			panelDetalle.add(new AnalisisDetallePanel(id));
		}
	}
	
	private class AsignTurnoClickHandler implements ClickHandler {
		private AnalisisDTO analisis;
		public AsignTurnoClickHandler(AnalisisDTO analis) {
			this.analisis = analis;
		}
		@Override
		public void onClick(ClickEvent arg0) {
			DialogBox dialogBox = createDialogBox();
			dialogBox.center();
			dialogBox.show();
		}
		private DialogBox createDialogBox() {
			final DialogBox box = new DialogBox();
			box.setText("Asignar turno");
			Button closeButton = new Button("Cancelar", new ClickHandler() {
				public void onClick(ClickEvent event) {
					box.hide();
				}
			});
			VerticalPanel container = new VerticalPanel();
			Panel errorTarget = new HorizontalPanel();
			AsignTurnoClickHandlerFactory handlerFactory = new AsignTurnoClickHandlerFactory(analisis, box, errorTarget);
			container.add(new TurnoSelectionPanel(new ChooseTurnoActionExecutor(handlerFactory)));
			container.add(closeButton);
			container.add(errorTarget);
			
			box.setGlassEnabled(true);
			box.setAnimationEnabled(true);
			box.add(container);
			return box;
		}
	}
}

